package com.book.controller;

import com.book.po.Book;
import com.book.service.bookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
@RequestMapping("/book")
public class bookController {
    @Autowired
    private bookService bookService;

    @RequestMapping("/book_select")
    public String book_select(RedirectAttributes  model, Book book){
        List<Book> books = bookService.book_select(book);
        if (books.size()>=1){
            model.addFlashAttribute("books",books);
            model.addFlashAttribute("msg","");
            return "redirect:/manage/bookManage";
        }else {
            model.addFlashAttribute("msg","没有查找到对应书籍！");
            return "redirect:/manage/bookManage";
        }
    }

    @RequestMapping("/book_insert")
    public String book_insert(RedirectAttributes  model, Book book){
        if (book!=null) {
            book.setCurrentNum(book.getNum());
        }
        String msg = bookService.book_insert(book);

        model.addFlashAttribute("msg",msg);
        return "redirect:/manage/bookManage";
    }

    @RequestMapping("/book_update")
    public String book_update(RedirectAttributes  model, Book book){
        String msg = bookService.book_update(book);
        model.addFlashAttribute("msg",msg);
        return "redirect:/manage/bookManage";
    }

    @RequestMapping("/book_delete")
    public String book_delete(RedirectAttributes  model, Integer bookId){
        String msg = bookService.book_delete(bookId);
        model.addFlashAttribute("msg",msg);
        return "redirect:/manage/bookManage";
    }
}
